package Stack;

public class MyListStack {
    private Node head = new Node();
    private int size = 0;

    public class Node {
        private Object data;
        private Node next;
    }

    //压栈 - push
    public void push(Object obj) {
        Node newNode = new Node();
        newNode.data = obj;
        newNode.next = head.next;
        head.next = newNode;
        size++;
    }

    //弹栈 - pull
    public Object pull() {
        Node node = head;
        while (node.next != null) {
            Node pullNode = head.next;
            head.next = head.next.next;
            return pullNode.data;
        }
        return null;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        Node node = head;
        while (node.next != null) {
            node = node.next;
            sb.append(node.data + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        return sb.toString();
    }
}